package com.valuegroup.smarttodo.plugins.pickdialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.plugins.pickdialog.bean.DateType;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 日期选择器
 * Created by codbking on 2016/8/11.
 */
public class DatePickDialog extends Dialog implements OnChangeLisener {

    private FrameLayout wheelLayout;
    private TextView cancel;
    private TextView sure;
    private TextView messgeTv;

    private DateType type = DateType.TYPE_ALL;

    //开始时间
    private Date startDate = new Date();
    //年分限制，默认上下5年
    private int yearLimt = 5;

    // 初始数据
    private Object startData;

    private OnChangeLisener onChangeLisener;

    private OnSureLisener onSureLisener;

    // 自定义返回值
    private OnCustomSureLisener onCustomSureLisener;

    private DatePicker mDatePicker;

    //设置模式
    public void setType(DateType type) {
        this.type = type;
    }

    //设置开始时间(DateType.TYPE_*)
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    // 设置初始值（DateType.TYPE_CUSTOM_*）
    public void setStartData(Object startData) {
        this.startData = startData;
    }

    //设置年份限制，上下年份
    public void setYearLimt(int yearLimt) {
        this.yearLimt = yearLimt;
    }

    //设置选择回调
    public void setOnChangeLisener(OnChangeLisener onChangeLisener) {
        this.onChangeLisener = onChangeLisener;
    }

    //设置点击确定按钮，回调
    public void setOnSureLisener(OnSureLisener onSureLisener) {
        this.onSureLisener = onSureLisener;
    }

    //设置点击确定按钮，回调
    public void setOnCustomSureLisener(OnCustomSureLisener onCustomSureLisener) {
        this.onCustomSureLisener = onCustomSureLisener;
    }

    public DatePickDialog(Context context) {
        super(context, R.style.dialog_style);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cbk_dialog_pick_time);

        initView();
        initParas();
    }

    private DatePicker getDatePicker() {
        DatePicker picker = new DatePicker(getContext(), type);
        if (type.name().contains("CUSTOM")) {
            picker.setStartData(startData);
        } else {
            picker.setStartDate(startDate);
        }
        picker.setYearLimt(yearLimt);
        picker.setOnChangeLisener(this);
        picker.init();
        return picker;
    }

    private void initView() {
        this.sure = (TextView) findViewById(R.id.sure);
        this.cancel = (TextView) findViewById(R.id.cancel);
        this.wheelLayout = (FrameLayout) findViewById(R.id.wheelLayout);
        this.messgeTv = (TextView) findViewById(R.id.message);

        if (!type.getFormat().contains("dd")) {
            messgeTv.setVisibility(View.GONE);
        } else {
            messgeTv.setVisibility(View.VISIBLE);
        }

        mDatePicker = getDatePicker();
        this.wheelLayout.addView(mDatePicker);

        this.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        this.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

                if (type.name().contains("CUSTOM") && onCustomSureLisener != null) {
                    // 返回Object类型的值
                    switch (type) {
                        case TYPE_CUSTOM_MINUTE_INTERVAL_5:
                            onCustomSureLisener.onSure(mDatePicker.getMinuteInterval5());
                            break;
                        case TYPE_CUSTOM_QUARTER:
                            onCustomSureLisener.onSure(mDatePicker.getSelectQuarter());
                            break;
                        case TYPE_CUSTOM_YEAR_QUARTER:
                            onCustomSureLisener.onSure(mDatePicker.getSelectYearQuarter());
                            break;
                        case TYPE_CUSTOM_WEEK:
                            onCustomSureLisener.onSure(mDatePicker.getSelectWeek());
                            break;
                        case TYPE_CUSTOM_YEAR_WEEK:
                            onCustomSureLisener.onSure(mDatePicker.getSelectYearWeek());
                            break;
                        case TYPE_CUSTOM_DAY:
                            onCustomSureLisener.onSure(mDatePicker.getSelectDay());
                            break;
                        case TYPE_CUSTOM_NUMBER:
                            onCustomSureLisener.onSure(mDatePicker.getSelectCount());
                            break;
                    }
                } else {
                    // 返回Date类型的值
                    if (onSureLisener != null) {
                        onSureLisener.onSure(mDatePicker.getSelectDate());
                    }
                }
            }
        });
    }

    private void initParas() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        params.width = DateUtils.getScreenWidth(getContext()) - 140;
        getWindow().setAttributes(params);
    }

    @Override
    public void onChanged(Date date) {

        if (onChangeLisener != null) {
            onChangeLisener.onChanged(date);
        }

        // 含年月日的时候才显示星期
        if (type.getFormat().contains("dd")) {
            String messge = "";
            try {
                messge = new SimpleDateFormat("E").format(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
            messgeTv.setText(messge);
        }
    }

//    public static void main(String[] args) {
//        DatePickDialog dialog = new DatePickDialog(this);
//        //设置上下年分限制
//        dialog.setYearLimt(5);
//        //设置标题
//        dialog.setTitle("选择时间");
//        //设置类型
//        dialog.setType(DateType.TYPE_ALL);
//        //设置选择回调
//        dialog.setOnChangeLisener(null);
//        //设置点击确定按钮回调
//        dialog.setOnSureLisener(null);
//        dialog.showToast();
//    }
}
